﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Entities;
using DAL;

namespace SurveyGUI
{
    public partial class SurveyForm : Form
    {
        String connectionString;//khai bao ket noi
        SqlDataAdapter da;
        DataTable dt;

        public SurveyForm()
        {
            InitializeComponent();
        }

        private void SurveyForm_Load(object sender, EventArgs e)
        {
            
            LayThongTinLenForm();
            TongCauHoiKhaoSat();
        }
        private void TongCauHoiKhaoSat()
        {
            int s = 0;
            
            s += dgvDanhSachCauHoi.RowCount;
            toolStripStatusTongCauHoiKhaoSat.Text = "Tổng số câu hỏi khảo sát là: " + s;
                
        }
        private float DiemTrungBinh()
        {
            float diem = 0;
            float a = DiemKhaoSat();
            int SoCauHoi = dgvDanhSachCauHoi.RowCount;
            diem = (a / (SoCauHoi * 4)) * 100;
            return diem;

        }
        public float DiemKhaoSat()
        {
            int kq = 0;
            List<SubmitClass> list = GetValue();
            foreach (SubmitClass s in list)
            {
                kq += s.Diem;
            }

            return kq;

        }
        #region lấy thông tin từ dataGridView hiển thị lên form
        private void LayThongTinLenForm()
        {
            connectionString = "Data Source=TRONGTIEN-PC;Initial Catalog=Nhom15;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            da = new SqlDataAdapter("Select * from CauHoiKhaoSat", con);
            dt = new DataTable();
            da.Fill(dt);
            dgvDanhSachCauHoi.DataSource = dt;
        }
        #endregion
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!KiemTraThongTin())
                return;
            string gt = "";
            if (rdNam.Checked)
                gt = "Nam";
            else
                gt = "Nu";

            float diem = DiemTrungBinh();
            connectionString = "Data Source=TRONGTIEN-PC;Initial Catalog=Nhom15;Integrated Security=True";
            string query;
            SqlConnection conn = new SqlConnection(connectionString);
            try
            {
                conn.Open();
                query = "INSERT INTO KetQuaKhaoSat VALUES ('"+ lblMSSV.Text + "','"+ txtNganh.Text + "','"+ txtLop.Text + "','"+txtKhoaHoc.Text +"','"+(DateTime.Parse(dtpNgayKhaoSat.Value.ToString()))+"','"+ gt +"','" + diem + "','"+ txtYKienKhac.Text +"') ";
                SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                int i = sqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex) { MessageBox.Show(ex.StackTrace); }
            finally
            { conn.Close(); conn.Dispose(); }
            MessageBox.Show("khoa sat thanh cong");
            this.Close();
            
        }
        public bool KiemTraThongTin()
        {
            float diem = DiemTrungBinh();
            if (txtNganh.Text == "" && txtLop.Text == "" && txtKhoaHoc.Text == "" && rdNam.Checked == false && rdNu.Checked == false)
            {
                MessageBox.Show("chưa điền thông tin! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (txtNganh.Text == "")
            {
                MessageBox.Show("Chưa nhập ngành học! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (txtLop.Text == "")
            {
                MessageBox.Show("Chưa nhập lớp học! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (txtKhoaHoc.Text == "")
            {
                MessageBox.Show("Chưa nhập khóa học! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (rdNam.Checked == false && rdNu.Checked == false)
            {
                MessageBox.Show("Chưa nhập giới tính! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
             int s = dgvDanhSachCauHoi.RowCount;
             for (int i = 0; i < s; i++)
             {
                 DataGridViewRow dtnew = new DataGridViewRow();
                 dtnew = dgvDanhSachCauHoi.Rows[i];
                 if (Convert.ToBoolean(dtnew.Cells["RatDat"].Value) == false && Convert.ToBoolean(dtnew.Cells["Dat"].Value) == false && Convert.ToBoolean(dtnew.Cells["PhanVan"].Value) == false && Convert.ToBoolean(dtnew.Cells["ChuaDat"].Value) == false && Convert.ToBoolean(dtnew.Cells["HoanToanChuaDat"].Value) == false)
                 {
                     MessageBox.Show("Chưa trả lời hết câu hỏi! ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return false;
                 }
             }
            return true;
        }
        public List<SubmitClass> GetValue()
        {
            List<SubmitClass> list = new List<SubmitClass>();
            int s = dgvDanhSachCauHoi.RowCount;
            for (int i = 0; i < s; i++)
            {
                SubmitClass submitcs = new SubmitClass();//luu cau hoi nao co diem bao nhieu
                DataGridViewRow dtnew = new DataGridViewRow();               
                dtnew = dgvDanhSachCauHoi.Rows[i];
                submitcs.IDCauHoi = Convert.ToInt32(dtnew.Cells["IDCauHoi"].Value);
                if (Convert.ToBoolean(dtnew.Cells["RatDat"].Value) == true)
                {
                    submitcs.Diem = 4;
                }
                else  if (Convert.ToBoolean(dtnew.Cells["Dat"].Value) == true)
                {
                    submitcs.Diem = 3;
                }
                else if (Convert.ToBoolean(dtnew.Cells["PhanVan"].Value) == true)
                {
                    submitcs.Diem = 2;
                }
                else if (Convert.ToBoolean(dtnew.Cells["ChuaDat"].Value) == true)
                {
                    submitcs.Diem = 1;
                }
                else if (Convert.ToBoolean(dtnew.Cells["HoanToanChuaDat"].Value) == true)
                {
                    submitcs.Diem = 0;
                }
                list.Add(submitcs);
            }
            return list;
        }
        
        private void dgvDanhSachCauHoi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                {

                    if (e.ColumnIndex == 0)
                    {
                        
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[1].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[2].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[3].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[4].Value = false;
                    }
                    else if (e.ColumnIndex == 1)
                    {
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[0].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[2].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[3].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[4].Value = false;
                    }
                    else if (e.ColumnIndex == 2)
                    {
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[0].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[1].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[3].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[4].Value = false;
                    }
                    else if (e.ColumnIndex == 3)
                    {

                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[0].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[1].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[2].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[4].Value = false;
                    }
                    else if (e.ColumnIndex == 4)
                    {
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[0].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[1].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[2].Value = false;
                        dgvDanhSachCauHoi.Rows[e.RowIndex].Cells[3].Value = false;
                        
                    }
                    
                }
                
            }

        

    }
}
